package com.zf.museum.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

@Component
public class OpenIdUtil {
    private String appId = "wx43263457a59c4bd5";

    private String secret = "7fc1f3735fd273366ef66209737d5a1d";

    public  String getOpenid(String code) {

        //授权（必填）
        String grant_type = "authorization_code";
        //URL
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        //请求参数
        String params = "appid=" + appId + "&secret=" + secret + "&js_code=" + code + "&grant_type=" + grant_type;
        //发送请求
        String data = HttpUtil.get(requestUrl, params);
        //解析相应内容（转换成json对象）
        JSONObject json = JSONObject.parseObject(data);
        //用户的唯一标识（openid）
        String openId = String.valueOf(json.get("openid"));
        //System.out.println(Openid);
        return openId;
    }


}
